import 'package:dio/dio.dart';

abstract class DioInterceptor extends Interceptor {
  @override
  Future<void> onRequest(
      RequestOptions options, RequestInterceptorHandler handler) async {
    handler.next(await handleRequest(options)); // 继续请求
  }

  @override
  Future<void> onResponse(
      Response response, ResponseInterceptorHandler handler) async {
    handler.next(await handleResponse(response)); // 继续响应
  }

  @override
  Future<void> onError(DioError err, ErrorInterceptorHandler handler) async {
    handler.next(await handleError(err));
  }

  /// 可被子类重写的方法
  Future<RequestOptions> handleRequest(RequestOptions options) async {
    return options;
  }

  /// 可被子类重写的方法
  Future<Response> handleResponse(Response response) async {
    return response;
  }

  /// 可被子类重写的错误处理方法
  Future<DioError> handleError(DioError err) async {
    // 基础实现只返回原错误
    return err;
  }
}
